package com.xiaozhen.blog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaozhen.blog.entity.Type;
import com.xiaozhen.blog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @create 2023-05-08 10:55
 * 分类的添加删除
 */
@Controller
@RequestMapping("/admin")
public class TypeAdminController {

    @Autowired
    private TypeService typeService;

    /**
     * 点击导航栏的分类
     *
     * @param model
     * @return
     */
    @GetMapping("/types")
    @PreAuthorize("hasAuthority('admin')")
    public String types(@RequestParam(value = "page", required = false) Long page, Model model) {
        model.addAttribute("page", typeService.typePage(page));
        return "admin/types";
    }

    /**
     * 点击新增按钮
     *
     * @param model
     * @return
     */
    @GetMapping("/types/input")
    @PreAuthorize("hasAuthority('admin')")
    public String addType(Model model) {
        model.addAttribute("type", new Type());
        return "admin/types-input";
    }

    /**
     * 新增博客分类
     * @param type
     * @param result
     * @param attributes
     * @return
     */
    @PostMapping("/types")
    @PreAuthorize("hasAuthority('admin')")
    public String inputType(@Validated Type type, BindingResult result, RedirectAttributes attributes){
        // 判断新增分类有没有重复的
        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Type :: getName,type.getName());
        int count = typeService.count(wrapper);
        if (count != 0){
            result.rejectValue("name", "nameError", "不能重复添加分类");
            return "admin/types-input";
        }
        // 如果不重复，保存到数据库
        boolean save = typeService.save(type);
        if (!save) {
            attributes.addFlashAttribute("message", "新增失败");
        } else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/types";
    }

    /**
     * 点击编辑按钮
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/types/{id}/input")
    @PreAuthorize("hasAuthority('admin')")
    public String editInput(@PathVariable Long id, Model model) {
        model.addAttribute("type", typeService.getById(id));
        return "admin/types-input";
    }

    /**
     * 修改博客分类
     * @param type
     * @param result
     * @param id
     * @param attributes
     * @return
     */
    @PostMapping("/types/{id}")
    @PreAuthorize("hasAuthority('admin')")
    public String editPost(@Validated Type type, BindingResult result, @PathVariable Long id, RedirectAttributes attributes) {
        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Type :: getName,type.getName());
        int count = typeService.count(wrapper);
        if (count != 0){
            result.rejectValue("name", "nameError", "不能修改分类为其它分类一样的名称！");
            return "admin/types-input";
        }
        boolean update = typeService.updateById(type);
        if (!update) {
            attributes.addFlashAttribute("message", "更新失败");
        } else {
            attributes.addFlashAttribute("message", "更新成功");
        }
        return "redirect:/admin/types";
    }

    /**
     *
     * @param id
     * @param attributes 返回信息
     * @return
     */
    @GetMapping("/types/{id}/delete")
    @PreAuthorize("hasAuthority('admin')")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        typeService.removeById(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/types";
    }
}
